package com.xiaoqiang.mybatis.study.spring;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.*;
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author 小强
 * @since 2019/1/2 20:36
 */
public class MybatisSpringAnnotationConfigDemo {
    private static ApplicationContext context;

    static {
        context = new AnnotationConfigApplicationContext(Application.class);
    }

    public static void main(String[] args) throws IOException {

        System.out.println();
    }

    @Configuration
    @ComponentScan
    @MapperScan("com.xiaoqiang.mybatis.study.spring.dao")
    public static class Application implements ApplicationContextAware {
        private ApplicationContext context;

        @Bean
        public PropertyPlaceholderConfigurer dbProps() {
            ClassPathResource resource = new ClassPathResource("db.properties");
            PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
            configurer.setLocation(resource);
            return configurer;
        }

        @Bean
        public DataSource dataSource(@Value("${db.username}") String username,
                                     @Value("${db.password}") String password,
                                     @Value("${db.url}") String url,
                                     @Value("${db.driver}") String driver) {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setUrl(url);
            dataSource.setDriverClassName(driver);
            return dataSource;
        }

        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            try {
                factoryBean.setMapperLocations(context.getResources("mappers/*.xml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            factoryBean.setDataSource(dataSource);
            return factoryBean;
        }

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;
        }
    }

    public static class A {

    }
}
